<template>
  <!-- 单价 -->
  <div>
    <el-input
      :placeholder="$t('请输入单价')"
      :disabled="readonly || disabledFlag"
      style="width: 27%"
      @change="changeData"
      v-model="checkedData"
    ></el-input>
  </div>
</template>
<script>
import nonStandardService from '@/services/nonStandardService'
import eventBus from '@/assets/utils/eventBus.js'
export default {
  name: 'unitPrices',
  props: {
    detail: {
      type: Object,
      default: () => {
        return {}
      },
    },
    value: [Object, String],
    disabled: Boolean,
    parameter: {
      type: Object,
      default: () => {
        return {}
      },
    },
    data: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      checkedData: '',
      rules: {
        checkedData: [
          {
            required: this.parameter.inputFlag === 1 && !this.disabledFlag,
            validator: (rule, value, callback) => {
              if (
                this.checkedData &&
                !/^\d{1,13}(\.\d{1,2})?$/.test(this.checkedData)
              ) {
                callback(
                  new Error(
                    this.$t('请输入小数点前13位以内，小数点后2位以内的数字')
                  )
                )
                return
              }
              if (
                !this.checkedData &&
                this.parameter.inputFlag === 1 &&
                !this.disabledFlag
              ) {
                callback(new Error(this.$t('请输入单价')))
                return
              } else {
                callback()
                return
              }
            },
            trigger: 'blur',
          },
        ],
      },
      disabledFlag: false,
      readonly: false,
    }
  },
  created() {
    this.readonly = Boolean(this.$route.query.readonly)
    if (this.value) {
      this.setData()
    }
    this.$emit('setrules', this.parameter, {
      [this.parameter.parameterNumber]: this.rules.checkedData,
    })
    /**在订单维护：复选按钮，0否、1是
     * 勾选后支持在订单维护单价，会清空单价，并置灰，不校验必填
     */
    eventBus.$on('orderUpholdFlagChange', (val) => {
      this.disabledFlag = val === 1
      this.checkedData = ''
    })
  },
  methods: {
    changeData() {
      this.$emit('input', String(this.checkedData))
    },
    setData() {
      let orderUpholdFlag =
        this.data.orderUpholdFlag &&
        this.data.orderUpholdFlag.indexOf('{') === 0
          ? JSON.parse(this.data.orderUpholdFlag)
          : {}
      this.disabledFlag = orderUpholdFlag.checkedData == 1
      this.checkedData = JSON.parse(this.value).value
        ? JSON.parse(this.value).value
        : ''
    },
    getData() {
      return {
        value: this.checkedData,
      }
    },
  },
}
</script>
